package com.dts.yjnb.service.service.impl;

import com.dts.common.core.page.TableDataInfo;
import com.dts.common.exception.ServiceException;
import com.dts.common.utils.DateUtils;
import com.dts.yjnb.service.domain.Agricultural;
import com.dts.yjnb.service.domainDto.AgriculturalDTO;
import com.dts.yjnb.service.mapper.AgriculturalMapper;
import com.dts.yjnb.service.service.IAgriculturalService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 农事服务 服务层实现
 *
 * @author dts
 */
@Service
public class AgriculturalServiceImpl implements IAgriculturalService
{
    @Autowired
    private AgriculturalMapper agriculturalMapper;

    /**
     * 农事服务分页查询
     */
    @Override
    public TableDataInfo pageQuery(AgriculturalDTO agriculturalDTO)
    {
        //属性拷贝
        Agricultural agricultural = new Agricultural();
        BeanUtils.copyProperties(agriculturalDTO, agricultural);

        // 开始分页
        PageHelper.startPage(agriculturalDTO.getPage(), agriculturalDTO.getPageSize());
        Page<Agricultural> page = agriculturalMapper.pageQuery(agricultural);

        long total = page.getTotal();
        List<Agricultural> records = page.getResult();

        // 封装返回结果
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(total);
        tableDataInfo.setRows(records);

        return tableDataInfo;
    }

    /**
     * 根据ID查询农事服务详情
     */
    @Override
    public Agricultural getByid(Long id)
    {
        // 参数校验
        if (id == null) {
            throw new ServiceException("农事服务ID不能为空");
        }
        
        // 查询详情
        Agricultural agricultural = agriculturalMapper.getByid(id);
        
        // 校验查询结果
        if (agricultural == null) {
            throw new ServiceException("农事服务不存在");
        }
        
        return agricultural;
    }

    /**
     * 批量删除农事服务
     */
    @Override
    public void deleteByids(List<Long> ids)
    {
        //参数校验
        if (ids == null || ids.size() == 0) {
            throw new ServiceException("请选择要删除的农事服务");
        }
        
        //执行删除
        Integer count = agriculturalMapper.deleteByids(ids);
        if (count == 0) {
            throw new ServiceException("删除失败，农事服务不存在");
        }
    }

    /**
     * 批量修改农事服务状态
     */
    @Override
    public void updateStatusByids(List<Long> ids, Integer status)
    {
        //参数校验
        if (ids == null || ids.size() == 0) {
            throw new ServiceException("请选择要操作的农事服务");
        }
        //校验状态参数
        if (status == null || (status != 0 && status != 1)) {
            throw new ServiceException("状态参数错误");
        }
        
        //执行修改
        Integer count = agriculturalMapper.updateStatusByids(ids, status);
        if (count == 0) {
            throw new ServiceException("修改失败，农事服务不存在");
        }
    }

    /**
     * 批量修改农事服务推荐状态
     */
    @Override
    public void updateRecommendByids(List<Long> ids, Integer isRecommend)
    {
        //参数校验
        if (ids == null || ids.size() == 0) {
            throw new ServiceException("请选择要操作的农事服务");
        }
        //校验推荐状态参数
        if (isRecommend == null || (isRecommend != 0 && isRecommend != 1)) {
            throw new ServiceException("推荐状态参数错误");
        }
        
        //执行修改
        Integer count = agriculturalMapper.updateRecommendByids(ids, isRecommend);
        if (count == 0) {
            throw new ServiceException("修改失败，农事服务不存在");
        }
    }

    /**
     * 新增农事服务
     */
    @Override
    public boolean add(AgriculturalDTO agriculturalDTO)
    {
        //参数校验
        if (agriculturalDTO == null) {
            throw new ServiceException("农事服务信息不能为空");
        }
        if (agriculturalDTO.getTitle() == null || agriculturalDTO.getTitle().isEmpty()) {
            throw new ServiceException("农事服务标题不能为空");
        }
        
        //DTO转换为实体类
        Agricultural agricultural = new Agricultural();
        BeanUtils.copyProperties(agriculturalDTO, agricultural);
        agricultural.setCreateTime(DateUtils.getNowDate());
        agricultural.setUpdateTime(DateUtils.getNowDate());
        
        //执行新增
        Integer count = agriculturalMapper.add(agricultural);
        
        //校验新增结果
        if (count == 0) {
            throw new ServiceException("新增失败");
        }
        
        return true;
    }

    /**
     * 修改农事服务
     */
    @Override
    public boolean update(AgriculturalDTO agriculturalDTO)
    {
        //参数校验
        if (agriculturalDTO == null) {
            throw new ServiceException("农事服务信息不能为空");
        }
        if (agriculturalDTO.getId() == null) {
            throw new ServiceException("农事服务ID不能为空");
        }
        
        //DTO转换为实体类
        Agricultural agricultural = new Agricultural();
        BeanUtils.copyProperties(agriculturalDTO, agricultural);
        agricultural.setUpdateTime(DateUtils.getNowDate());
        
        //执行更新
        Integer count = agriculturalMapper.update(agricultural);
        
        //校验更新结果
        if (count == 0) {
            throw new ServiceException("更新失败，农事服务不存在");
        }
        
        return true;
    }

    /**
     * 根据条件模糊查询农事服务
     */
    @Override
    public TableDataInfo search(AgriculturalDTO agriculturalDTO)
    {
        //属性拷贝
        Agricultural agricultural = new Agricultural();
        BeanUtils.copyProperties(agriculturalDTO, agricultural);

        // 开始分页
        PageHelper.startPage(agriculturalDTO.getPage(), agriculturalDTO.getPageSize());
        List<Agricultural> list = agriculturalMapper.search(agricultural);
        // 将List转换为Page对象以获取分页信息
        Page<Agricultural> page = (Page<Agricultural>) list;

        long total = page.getTotal();
        List<Agricultural> records = page.getResult();

        // 封装返回结果
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(total);
        tableDataInfo.setRows(records);

        return tableDataInfo;
    }
}
